package com.chii.drink;

import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.widget.TextView;

import com.chii.drink.fragment.DrinkFragment;
import com.chii.drink.fragment.SettleFragment;
import com.chii.drink.fragment.StatisticsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.message)
    TextView mTextMessage;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        viewPager.addOnPageChangeListener(mOnPageChangeListener);
    }

    private void initView() {
        fragments = new ArrayList<>();
        fragments.add(new DrinkFragment());
        fragments.add(new StatisticsFragment());
        fragments.add(new SettleFragment());
        MFragmentAdapter mFragmentAdapter = new MFragmentAdapter(getSupportFragmentManager(),
                this, fragments);
        viewPager.setAdapter(mFragmentAdapter);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_drink:
                    mTextMessage.setText(R.string.title_home);
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_statistics:
                    mTextMessage.setText(R.string.title_dashboard);
                    viewPager.setCurrentItem(1);

                    return true;
                case R.id.navigation_settle:
                    mTextMessage.setText(R.string.title_notifications);
                    viewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };
    private ViewPager.OnPageChangeListener mOnPageChangeListener =
            new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {
                    //注意这个方法滑动时会调用多次，下面是参数解释：
                    //position当前所处页面索引,滑动调用的最后一次绝对是滑动停止所在页面
                    //positionOffset:表示从位置的页面偏移的[0,1]的值。
                    //positionOffsetPixels:以像素为单位的值，表示与位置的偏移
                }

                @Override
                public void onPageSelected(int i) {
                    //该方法只在滑动停止时调用，position滑动停止所在页面位置
                    //当滑动到某一位置，导航栏对应位置被按下
                    navView.getMenu().getItem(i).setChecked(true);
                    //这里使用navigation.setSelectedItemId(position);无效，
                    //setSelectedItemId(position)的官网原句：Set the selected
                    // menu item ID. This behaves the same as tapping on an item
                    //未找到原因
                }

                @Override
                public void onPageScrollStateChanged(int i) {
                    //这个方法在滑动是调用三次，分别对应下面三种状态
                    // 这个方法对于发现用户何时开始拖动，
                    // 何时寻呼机自动调整到当前页面，或何时完全停止/空闲非常有用。
                    //                state表示新的滑动状态，有三个值：
                    //                SCROLL_STATE_IDLE：开始滑动（空闲状态->滑动），实际值为0
                    //                SCROLL_STATE_DRAGGING：正在被拖动，实际值为1
                    //                SCROLL_STATE_SETTLING：拖动结束,实际值为2
                }
            };
}
